package com.siwei.mes.sync.erp;

import com.alibaba.fastjson.JSON;
import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.base.BaseParams;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.bus.SyncGroup;
import com.siwei.mes.entity.sync.erp.dto.ErpCustomerDto;
import com.siwei.mes.mapper.syncErp.ErpCustomerMapper;
import com.siwei.mes.service.laboratory.CustomInfoService;
import com.siwei.mes.service.sync.erp.ErpCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 对外接口-客户信息表相关
 *
 * @author linzi
 * @since 2024-07-25 15:26:43
 */
@Api(value = "客户信息表")
@RestController
@RequestMapping("erp/customer")
public class SyncErpCustomerController {
    private static final Logger log = LoggerFactory.getLogger("erp");

    @Value("${erp-token}")
    private String ERP_TOKEN;
    @Resource
    private ErpCustomerService erpCustomerService;
    @Resource
    private CustomInfoService customInfoService;
    @Resource
    private ErpCustomerMapper erpCustomerMapper;

    /**
     * 校验token
     *
     * @param request
     */
    public void checkToken(HttpServletRequest request) {
        String token = request.getHeader(BaseParams.Authorization);
        assertTrue(StringUtils.isBlank(token), "授权token为空");
        assertTrue(!token.equals(ERP_TOKEN), "token不正确");
    }

    @AnonymousAccess
    @ApiOperation(value = "新增或修改客户信息表")
    @PostMapping("/saveOrUpdate")
    public Json saveOrUpdate(@RequestBody @Validated(SyncGroup.class) ErpCustomerDto erpCustomerDto, HttpServletRequest request) {
        log.info("ERP新增或修改客户信息，入参：{}", JSON.toJSONString(erpCustomerDto));
        log.info("ERP新增或修改客户信息已禁用，在任务单的时候根据Fhtdw新增客户");
        //this.checkToken(request);
        //ErpCustomer erpCustomer = null;
        //try {
        //    erpCustomer = erpCustomerService.erpSaveOrUpdate(erpCustomerDto);
        //} catch (Exception e) {
        //    log.info("ERP新增或修改客户信息失败，错误原因：【{}】", e.getMessage(), e);
        //    /**手动回滚事务*/
        //    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        //    assertTrue("ERP新增或修改客户信息失败，错误原因：" + e.getMessage() + "】");
        //}
        //
        ///**这里操作协会的*/
        //String laboratoryId = customInfoService.saveOrUpdate(erpCustomer, false);
        //if (StringUtils.isNotBlank(laboratoryId) && erpCustomer != null) {
        //    erpCustomerMapper.updateByPrimaryKeySelective(new ErpCustomer().setId(erpCustomer.getId()).setLaboratoryId(laboratoryId).setUpdateTime(new Date()));
        //}
        return Json.succ();
    }

    /**
     * 目前没有删除接口
     * @param entity
     * @param request
     * @return
     */
    //@AnonymousAccess
    //@ApiOperation(value = "删除客户信息表")
    //@PostMapping("/deleteByErpId")
    //public Json deleteByErpId(@RequestBody ErpCustomer entity, HttpServletRequest request) {
    //    log.info("ERP删除客户信息，入参：{}", JSON.toJSONString(entity));
    //    this.checkToken(request);
    //    assertTrue(StringUtils.isBlank(entity.getErpId()), "ERP系统唯一标识为空");
    //    ErpCustomer erpCustomer = erpCustomerService.deleteByErpId(entity.getErpId());
    //
    //    /**删除协会的*/
    //    if (erpCustomer != null) {
    //        customInfoService.delectByCustId(erpCustomer.getLaboratoryId());
    //    }
    //    return Json.succ().data(new ApiResult());
    //}
}
